import { apiClient } from './apiClient'
import { ApiResponse } from '@/types/api'
import { Version } from '@/types/version'

export interface CreateVersionRequest {
  script_id: string
  comment?: string
  is_major?: boolean
  created_by: string
}

export interface UpdateVersionRequest {
  comment?: string
  is_major?: boolean
}

export interface VersionHistoryResponse {
  versions: Version[]
  total: number
  current_version_id?: string
}

export interface VersionCompareResponse {
  version1: Version
  version2: Version
  diff: {
    added_lines: number
    removed_lines: number
    modified_lines: number
    unchanged_lines: number
    changes: Array<{
      type: 'added' | 'removed' | 'modified'
      line_number: number
      content: string
      old_content?: string
      new_content?: string
    }>
  }
}

/**
 * 获取脚本版本历史
 */
export const getVersionHistory = async (
  scriptId: string,
  params?: {
    limit?: number
    offset?: number
    include_content?: boolean
  }
): Promise<ApiResponse<VersionHistoryResponse>> => {
  const response = await apiClient.get(`/api/versions/scripts/${scriptId}/versions`, { params })
  return response.data
}

/**
 * 获取单个版本详情
 */
export const getVersion = async (versionId: string): Promise<ApiResponse<Version>> => {
  const response = await apiClient.get(`/api/versions/${versionId}`)
  return response.data
}

/**
 * 创建新版本
 */
export const createVersion = async (data: CreateVersionRequest): Promise<ApiResponse<Version>> => {
  const response = await apiClient.post('/api/versions', data)
  return response.data
}

/**
 * 更新版本信息
 */
export const updateVersion = async (
  versionId: string, 
  data: UpdateVersionRequest
): Promise<ApiResponse<Version>> => {
  const response = await apiClient.put(`/api/versions/${versionId}`, data)
  return response.data
}

/**
 * 删除版本
 */
export const deleteVersion = async (versionId: string): Promise<ApiResponse<void>> => {
  const response = await apiClient.delete(`/api/versions/${versionId}`)
  return response.data
}

/**
 * 回滚到指定版本
 */
export const revertToVersion = async (
  scriptId: string, 
  versionId: string
): Promise<ApiResponse<Version>> => {
  const response = await apiClient.post(`/api/versions/scripts/${scriptId}/revert`, {
    version_id: versionId
  })
  return response.data
}

/**
 * 比较两个版本
 */
export const compareVersions = async (
  version1Id: string, 
  version2Id: string
): Promise<ApiResponse<VersionCompareResponse>> => {
  const response = await apiClient.get(`/api/versions/compare`, {
    params: {
      version1_id: version1Id,
      version2_id: version2Id
    }
  })
  return response.data
}

/**
 * 更新版本备注
 */
export const updateVersionComment = async (
  versionId: string, 
  comment: string
): Promise<ApiResponse<Version>> => {
  const response = await apiClient.patch(`/api/versions/${versionId}/comment`, { comment })
  return response.data
}

/**
 * 获取版本差异
 */
export const getVersionDiff = async (
  version1Id: string, 
  version2Id: string,
  options?: {
    ignore_whitespace?: boolean
    context_lines?: number
  }
): Promise<ApiResponse<{
  diff_text: string
  stats: {
    added_lines: number
    removed_lines: number
    modified_lines: number
    unchanged_lines: number
  }
}>> => {
  const response = await apiClient.get(`/api/versions/diff`, {
    params: {
      version1_id: version1Id,
      version2_id: version2Id,
      ...options
    }
  })
  return response.data
}

/**
 * 导出版本内容
 */
export const exportVersion = async (
  versionId: string, 
  format: 'sql' | 'txt' = 'sql'
): Promise<Blob> => {
  const response = await apiClient.get(`/api/versions/${versionId}/export`, {
    params: { format },
    responseType: 'blob'
  })
  return response.data
}

/**
 * 批量删除版本
 */
export const batchDeleteVersions = async (versionIds: string[]): Promise<ApiResponse<{
  success_count: number
  failed_count: number
  errors: string[]
}>> => {
  const response = await apiClient.post('/api/versions/batch-delete', {
    version_ids: versionIds
  })
  return response.data
}

/**
 * 获取版本统计信息
 */
export const getVersionStats = async (scriptId: string): Promise<ApiResponse<{
  total_versions: number
  major_versions: number
  minor_versions: number
  latest_version: Version
  version_timeline: Array<{
    date: string
    version_count: number
    major_count: number
  }>
}>> => {
  const response = await apiClient.get(`/api/versions/scripts/${scriptId}/stats`)
  return response.data
}

/**
 * 搜索版本
 */
export const searchVersions = async (
  scriptId: string,
  params: {
    keyword?: string
    created_by?: string
    start_date?: string
    end_date?: string
    is_major?: boolean
    limit?: number
    offset?: number
  }
): Promise<ApiResponse<VersionHistoryResponse>> => {
  const response = await apiClient.get(`/api/versions/scripts/${scriptId}/search`, { params })
  return response.data
}